<?php

/**
 * job actions.
 *
 * @package    maximous
 * @subpackage job
 * @author     Mina.Gerges.Wadea
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class jobActions extends MaxFrontendActions {

    public function preExecute() {
        parent::preExecute();
        if ($this->maxUser) {
            $this->maxUser->getMaxUserRole()->setDefaultSiteComponent(SiteComponent::Job);
        }
    }

    /**
     * Executes index action
     *
     * @param sfRequest $request A request object
     */
    public function executeIndex(sfWebRequest $request) {
        //check access
        $this->formFilter = new JobFrontendFormFilter();
        $this->formFilter->bind($request->getParameter($this->formFilter->getName()));
        $query = $this->formFilter->buildQuery($this->formFilter->getValues());
        $query->orderBy('id desc');
        $page = $request->getParameter('page', 1);
        $this->pager = $this->createPager('Job', $query, $page, sfConfig::get('app_record_per_page'));
    }

    public function executeSearch(sfWebRequest $request) {
        $this->redirect($this->generateUrl('HomePage', $request->getGetParameters()));
    }

    public function executeAddJob(sfWebRequest $request) {
        $this->forward404Unless($this->maxUser->getMaxUserRole()->canAdd(), $this->i18n->__("you dont't have permission to access this page"));
        $this->jobForm = new JobForm(null, array('maxUser' => $this->maxUser));
        if ($request->isMethod(sfWebRequest::POST)) {
            $jobFormData = $request->getParameter($this->jobForm->getName());
            $this->jobForm->bind($jobFormData);
            if ($this->jobForm->isValid()) {
                $this->jobForm->save();
                $this->getUser()->setFlash('message_success', $this->i18n->__('add job successful'));
            }
        }
    }

    public function executeEditJob(sfWebRequest $request) {
        $this->forward404Unless($this->maxUser->getMaxUserRole()->canEdit(), $this->i18n->__("you dont't have permission to access this page"));
        $job = $this->checkJob($request);
        $this->jobForm = new JobForm($job, array('maxUser' => $this->maxUser));
        if ($request->isMethod(sfWebRequest::POST)) {
            $jobFormData = $request->getParameter($this->jobForm->getName());
            $this->jobForm->bind($jobFormData);
            if ($this->jobForm->isValid()) {
                $this->jobForm->save();
                $this->getUser()->setFlash('message_success', $this->i18n->__('edit job successful'));
            }
        }
    }

    public function executeDeleteJob(sfWebRequest $request) {
        $this->forward404Unless($this->maxUser->getMaxUserRole()->canDelete(), $this->i18n->__("you dont't have permission to access this page"));
        $job = $this->checkJob($request);
        // delete job object
        $job->delete();
        $this->getUser()->setFlash('message_success', $this->i18n->__('edit job successful'));
        $this->redirect($request->getReferer());
    }

    public function executeShowMyJobs(sfWebRequest $request) {
        $userId = $this->maxUser->getUserDataByIndex('id');
        $page = $request->getParameter('page');
        $query = JobTable::getInstance()->getUserJobs($userId);
        $this->pager = $this->createPager('Job', $query, $page, sfConfig::get('app_home_jobs_per_page'));
    }

    public function executeShowJob(sfWebRequest $request) {
        $watchedJob = $this->getUser()->getAttribute('watched_job', array());
        $jobTitle = $request->getParameter('jtitle');
        $job = JobTable::getInstance()->findOneByTitle($jobTitle);
        $this->forward404Unless($job);
        if (!in_array($job->getId(), $watchedJob)) {
            $job->increamentVisits();
            $job->save();
            $watchedJob[] = $job->getId();
            $this->getUser()->setAttribute('watched_job', $watchedJob);
        }
        $this->job = $job;
    }

    /**
     *  check user if can access this job if not the function do forward404 else it return the job object
     * @param sfWebRequest $request
     * @return Job 
     */
    private function checkJob(sfWebRequest $request) {
        $jobTitle = $request->getParameter('jtitle');
        $userId = $this->maxUser->getUserDataByIndex('id');
        $job = JobTable::getInstance()->findOneByTitleAndUserId($jobTitle, $userId);
        $this->forward404Unless($job);
        return $job;
    }

}
